package com.example.testapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.testapplication.R;
import com.example.testapplication.entity.Food;

import java.util.List;

public class ListViewAdapter extends BaseAdapter {
    private List<Food> foodList;
    private LayoutInflater layoutInflater;

    public ListViewAdapter(List<Food> foodList, Context context){
        this.foodList = foodList;
        this.layoutInflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return foodList.size();
    }



    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHoler viewHoler;
        if(view == null){
            view = layoutInflater.inflate(R.layout.item_food,viewGroup,false);
            viewHoler = new ViewHoler();
            viewHoler.name = view.findViewById(R.id.name_tv);
            viewHoler.price = view.findViewById(R.id.price_tv);
            view.setTag(viewHoler);
        }else{
            viewHoler = (ViewHoler) view.getTag();
        }
        viewHoler.name.setText(foodList.get(i).getName());
        viewHoler.price.setText(String.valueOf(foodList.get(i).getPrice()+"元"));
        return view;
    }

    class ViewHoler{
        TextView name;
        TextView price;
    }
}
